<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ProductSalesSpecsModel extends Model {

    protected $table ="product_sales_specs";

    protected $pk = 'spec_id';

    private $cacheKey="product:specs:id:";

    private $cacheTime=5184000;


    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name('product_sales_specs')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('product_sales_specs')->field("spec_id")->where($where);


        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();
        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['spec_id']);
            }
            return $return;
        }
        return [];
    }


    public function getPorductSaleNumAndStock($product_id)
    {
       return Db::name('product_sales_specs')
            ->where('product_id', $product_id)
            ->field('SUM(sales_num) as total_sales_num, SUM(CASE WHEN spec_status=1 THEN stock ELSE 0 END) as total_stock')
            ->find();
    }



    /*
* 获取订单状态详情
* */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name('product_sales_specs')->find($id);
            $info['format_price'] = round( $info['price'] / 100, 2);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }
        $info["spec_status"] = $this->getSpecStatus($info["spec_id"]);
        $info["sales_num"] = $this->getSpecSalesNum($info["spec_id"]);
        $productInfo = (new ProductsModel())->getBaseInfo($info["product_id"]);
        $info["shop_id"] = $productInfo["shop_id"];
        if($productInfo["shop_info"]["shop_status"] != 1){
            $info['buy_status'] = -1;
            $info['buy_status_msg'] = "该店铺已关闭";
        }elseif ($productInfo["product_status"] != 1){
            $info['buy_status'] = -1;
            $info['buy_status_msg'] = "该商品已下架";
        }elseif ($info["spec_status"] != 1){
            $info['buy_status'] = -1;
            $info['buy_status_msg'] = "该规格商品已下架";
        }elseif($info["stock"] <= $info["sales_num"]) {
            $info['buy_status'] = -1;
            $info['buy_status_msg'] = "商品库存不足";
        }else{
            $info['buy_status'] = 1;
            $info['buy_status_msg'] = "可以购买";
        }

        return $info;
    }


    /*
 * 获取规格状态
 * */
    public function getSpecStatus($id){
        if(empty($id) ){
            return false;
        }
        $where=[];
        $where[] = ["spec_id","=",$id];
        return Db::name('product_sales_specs')->where($where)->value("spec_status");
    }

    /*
    * 获取规格数量
    * */
    public function getSpecSalesNum($id){
        if(empty($id) ){
            return false;
        }
        $where=[];
        $where[] = ["spec_id","=",$id];
        return Db::name('product_sales_specs')->where($where)->value("sales_num");
    }


    /*
     * 获取商品规格列表
     * */
    public function getProductSalesSpecsList($product_id){
        if(!$product_id) return [];

        $where=[];
        $where[] = ["product_id","=",$product_id];
        $where[] = ["spec_status","=",1];
        return $this->getList($where);
    }

    /*
 * 获取商品库存
 * */
    public function getProductStock($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        $where[] = ["spec_status","=",1];
        return Db::name('product_sales_specs')->where($where)->sum("stock");
    }


    /*
     * 获取商品销量
     * */
    public function getProductSalesNum($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        return Db::name('product_sales_specs')->where($where)->sum("sales_num");
    }


    /*
    * 批量操作
    * */
    public function bacthAdd($list){
        return Db::name('product_sales_specs')->insertAll($list);
    }

    /*
     * 删除商品所有规格
     * */
    public function delAllProductSalesSpecs($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        return $this->delAll($where);
    }


    /*
     * 更新规格销售
     * */
    public function incrProductSalesNum($spec_id,$num){
        $where =[];
        $where[] = ["spec_id","=",$spec_id];
        return Db::name('product_sales_specs')->where($where)->inc("sales_num",$num)->update();
    }


    /*
     * 删除操作
     * */
    public function delAll($where){
        return Db::name('product_sales_specs')->where($where)->delete();
    }


    /*
* 删除指定商品的缓存
**/

    public function delProductSalesSpecsCache($product_id){
        $where =[];
        $where[] = ["product_id","=",$product_id];
        $list = Db::name('product_sales_specs')->field("spec_id")->where($where)->select();
        if($list){
            $redisModel = new RedisModel();
            foreach($list as $item){
                $redisModel->delete($this->cacheKey.$item["spec_id"]);
            }
        }
    }


    /*
   * 删除全部商品的缓存
   **/

    public function delAllProductSalesSpecsCache(){
        $redisModel = new RedisModel();
        $redisModel->deleteAll($this->cacheKey);
    }

}